//
//  OperationColor.m
//  Blue-toothLight
//
//  Created by mazg on 15/6/16.
//  Copyright (c) 2015年 mazg. All rights reserved.
//

#import "OperationColor.h"
#import "LightModel.h"
#import "NSManagedObject+helper.h"

static OperationColor *operationColorInstance=nil;

@implementation OperationColor


-(NSMutableArray *)changeColorArr{
    if (!_changeColorArr) {
        _changeColorArr=[[NSMutableArray alloc]init];
    }
    return _changeColorArr;
}


+(instancetype)shareOperationColorInstance{
    if (operationColorInstance==nil) {
        operationColorInstance=[[OperationColor alloc]init];
    }
    return operationColorInstance;
}

//变换到目标色的方法
-(void)changeColorWithPeripheral:(CBPeripheral *)peripheral ToTarget:(UIColor *)targetColor{
    self.alwaysChange=NO;
    NSArray *services=peripheral.services;
    CGFloat red,green,blue,alpha;
    [targetColor getRed:&red green:&green blue:&blue alpha:&alpha];
    for (CBService *se in services) {
        for (CBCharacteristic *ch in se.characteristics) {
            if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                Byte byte3[] = {22,0,0,0,0};
                byte3[1]=alpha*255.0f;//白色
                byte3[2]=green*255.0f;//绿色
                byte3[3]=blue*255.0f;//蓝色
                byte3[4]=red*255.0f;//红色
                //传的顺序为 w,g,b,r
                NSData *data ;
                data = [NSData dataWithBytes:byte3 length:5];
                [peripheral writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
            }
        }
    }
}

//变换到目标色的方法
-(void)changeColorWithPeripheral:(CBPeripheral *)peripheral ToTarget:(UIColor *)targetColor afterDelay:(NSTimeInterval)delay{
    [NSThread sleepForTimeInterval:delay];
    self.alwaysChange=NO;
    NSArray *services=peripheral.services;
    CGFloat red,green,blue,alpha;
    [targetColor getRed:&red green:&green blue:&blue alpha:&alpha];
    for (CBService *se in services) {
        for (CBCharacteristic *ch in se.characteristics) {
            if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                Byte byte3[] = {22,0,0,0,0};
                byte3[1]=alpha*255.0f;//白色
                byte3[2]=green*255.0f;//绿色
                byte3[3]=blue*255.0f;//蓝色
                byte3[4]=red*255.0f;//红色
                //传的顺序为 w,g,b,r
                NSData *data ;
                data = [NSData dataWithBytes:byte3 length:5];
                [peripheral writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
            }
        }
    }
}

//渐变色的变化方法
-(void)changeColorWithPeripheral:(CBPeripheral *)peripheral ToTargetWith_Jianbian:(NSMutableArray *)changeColorArr{
    self.alwaysChange=NO;
    self.counter=0;
    _changeColorArr=changeColorArr;
    switch (changeColorArr.count) {
        case 3:
            {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.65 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    NSTimer *t=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeColorJianbian_WithTimes3:) userInfo:peripheral repeats:YES];
                    self.alwaysChange=YES;
                });
            }
            break;
        case 8:
            {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.65 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    NSTimer *t=[NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(changeColorJianbian_WithTimes8:) userInfo:peripheral repeats:YES];
                    self.alwaysChange=YES;
                });
            }
            break;
            
        default:
            break;
    }
}

//快闪的颜色的变化方法
-(void)changeColorWithPeripheral:(CBPeripheral *)peripheral ToTargetWith_Kuaishan:(NSMutableArray *)changeColorArr{
    self.alwaysChange=NO;
    self.counter=0;
    _changeColorArr=changeColorArr;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.65 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSTimer *t=[NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(changeColorKuaishan:) userInfo:peripheral repeats:YES];
        self.alwaysChange=YES;
    });
}

//慢闪颜色变化方法
-(void)changeColorWithPeripheral:(CBPeripheral *)peripheral ToTargetWith_Manshan:(NSMutableArray *)changeColorArr{
    self.alwaysChange=NO;
    self.counter=0;
    _changeColorArr=changeColorArr;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.65 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSTimer *t=[NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(changeColorManshan:) userInfo:peripheral repeats:YES];
        self.alwaysChange=YES;
    });
}


//开灯操作
-(void)openLightWithPeripheral:(CBPeripheral *)peripheral{
    self.alwaysChange=NO;
    NSArray *services=peripheral.services;
    for (CBService *se in services) {
        for (CBCharacteristic *ch in se.characteristics) {
            if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                Byte byte3[] = {22,0,0,0,0};
                byte3[1]=80;
                byte3[2]=80;
                byte3[3]=80;
                byte3[4]=80;
                
                NSData *data ;
                data = [NSData dataWithBytes:byte3 length:5];
                [peripheral writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
            }
        }
    }
}

//关灯操作
-(void)turnOffLightWithPeripheral:(CBPeripheral *)peripheral{
    NSArray *services=peripheral.services;
    self.alwaysChange=NO;
    //关灯的时候重置model数据
    NSArray *lightModelArr=[LightModel filter:nil orderby:nil offset:0 limit:0];
    for (LightModel *model in lightModelArr) {
        if ([model.perIdentifier isEqualToString:peripheral.identifier.UUIDString]) {
            model.perColor=[UIColor colorWithRed:80/255.0f green:80/255.0f blue:80/255.0f alpha:255/255.0f];
            [LightModel save:^(NSError *error) {
                
            }];
        }
    }
    //执行关灯操作
    for (CBService *se in services) {
        for (CBCharacteristic *ch in se.characteristics) {
            if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                Byte byte3[] = {20,0,0,0,0};
                byte3[1]=0;
                byte3[2]=0;
                byte3[3]=0;
                byte3[4]=0;
                NSData *data ;
                data = [NSData dataWithBytes:byte3 length:5];
                [peripheral writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
            }
        }
    }
}

//定时器，处理渐变色的方法(数组中三种颜色)
-(void)changeColorJianbian_WithTimes3:(NSTimer *)timer{
    if (self.alwaysChange) {
        self.counter++;
        UIColor *newColor;
        if (self.counter<=40) {
            newColor=[self getNewColorWithColor1:self.changeColorArr[0] andColor2:self.changeColorArr[1] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=80){
            newColor=[self getNewColorWithColor1:self.changeColorArr[1] andColor2:self.changeColorArr[2] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=120){
            newColor=[self getNewColorWithColor1:self.changeColorArr[2] andColor2:self.changeColorArr[0] andCurrentColor:self.currentColor];
            if (self.counter==120) {
                self.counter=0;
            }
        }
        CBPeripheral *cbp=[timer userInfo];
        NSArray *services=cbp.services;
        CGFloat red,green,blue,alpha;
        [newColor getRed:&red green:&green blue:&blue alpha:&alpha];
        for (CBService *se in services) {
            for (CBCharacteristic *ch in se.characteristics) {
                if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                    Byte byte3[] = {22,0,0,0,0};
                    byte3[1]=alpha*255.0f;//白色
                    byte3[2]=green*255.0f;//绿色
                    byte3[3]=blue*255.0f;//蓝色
                    byte3[4]=red*255.0f;//红色
                    //传的顺序为 w,g,b,r
                    NSData *data ;
                    data = [NSData dataWithBytes:byte3 length:5];
                    [cbp writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
                }
            }
        }
    }
    else{
        [timer invalidate];
        self.counter=0;
    }
}

//定时器，处理渐变色的方法(数组中八种颜色)
-(void)changeColorJianbian_WithTimes8:(NSTimer *)timer{
    if (self.alwaysChange) {
        self.counter++;
        UIColor *newColor;
        if (self.counter<=40) {
            newColor=[self getNewColorWithColor1:self.changeColorArr[0] andColor2:self.changeColorArr[1] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=80){
            newColor=[self getNewColorWithColor1:self.changeColorArr[1] andColor2:self.changeColorArr[2] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=120){
            newColor=[self getNewColorWithColor1:self.changeColorArr[2] andColor2:self.changeColorArr[3] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=160){
            newColor=[self getNewColorWithColor1:self.changeColorArr[3] andColor2:self.changeColorArr[4] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=200){
            newColor=[self getNewColorWithColor1:self.changeColorArr[4] andColor2:self.changeColorArr[5] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=240){
            newColor=[self getNewColorWithColor1:self.changeColorArr[5] andColor2:self.changeColorArr[6] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=280){
            newColor=[self getNewColorWithColor1:self.changeColorArr[6] andColor2:self.changeColorArr[7] andCurrentColor:self.currentColor];
        }
        else if (self.counter<=320){
            newColor=[self getNewColorWithColor1:self.changeColorArr[7] andColor2:self.changeColorArr[0] andCurrentColor:self.currentColor];
            if (self.counter==320) {
                self.counter=0;
            }
        }
        CBPeripheral *cbp=[timer userInfo];
        NSArray *services=cbp.services;
        CGFloat red,green,blue,alpha;
        [newColor getRed:&red green:&green blue:&blue alpha:&alpha];
        for (CBService *se in services) {
            for (CBCharacteristic *ch in se.characteristics) {
                if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                    Byte byte3[] = {22,0,0,0,0};
                    byte3[1]=alpha*255.0f;//白色
                    byte3[2]=green*255.0f;//绿色
                    byte3[3]=blue*255.0f;//蓝色
                    byte3[4]=red*255.0f;//红色
                    //传的顺序为 w,g,b,r
                    NSData *data ;
                    data = [NSData dataWithBytes:byte3 length:5];
                    [cbp writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
                }
            }
        }
    }
    else{
        [timer invalidate];
        self.counter=0;
    }
}

//定时器，快闪变色
-(void)changeColorKuaishan:(NSTimer *)timer{
    if (self.alwaysChange) {
        
        UIColor *newColor=self.changeColorArr[self.counter];
        self.counter++;
        if (self.counter==self.changeColorArr.count) {
            self.counter=0;
        }
        
        CBPeripheral *cbp=[timer userInfo];
        NSArray *services=cbp.services;
        CGFloat red,green,blue,alpha;
        [newColor getRed:&red green:&green blue:&blue alpha:&alpha];
        for (CBService *se in services) {
            for (CBCharacteristic *ch in se.characteristics) {
                if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                    Byte byte3[] = {22,0,0,0,0};
                    byte3[1]=0.0f;//白色
                    byte3[2]=green*255.0f;//绿色
                    byte3[3]=blue*255.0f;//蓝色
                    byte3[4]=red*255.0f;//红色
                    
                    //传的顺序为 w,g,b,r
                    NSData *data ;
                    data = [NSData dataWithBytes:byte3 length:5];
                    [cbp writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
                }
            }
        }
    }
    else{
        [timer invalidate];
        self.counter=0;
    }
}

//定时器，慢闪颜色变化方法
-(void)changeColorManshan:(NSTimer *)timer{
    if (self.alwaysChange) {
        self.counter++;
        UIColor *toColor=[UIColor colorWithRed:10.0/255.0f green:10.0/255.0f blue:10.0/255.0f alpha:1.0];
        UIColor *newColor;
        if (self.counter<=40) {
            if (self.counter==1) {
                self.currentColor=self.changeColorArr[0];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[0] andColor2:toColor andCurrentColor:self.currentColor];
        }
        else if (self.counter<=80){
            if (self.counter==41) {
                self.currentColor=self.changeColorArr[1];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[1] andColor2:toColor andCurrentColor:self.currentColor];
        }
        else if (self.counter<=120){
            if (self.counter==81) {
                self.currentColor=self.changeColorArr[2];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[2] andColor2:toColor andCurrentColor:self.currentColor];
        }
        else if (self.counter<=160){
            if (self.counter==121) {
                self.currentColor=self.changeColorArr[3];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[3] andColor2:toColor andCurrentColor:self.currentColor];
        }
        else if (self.counter<=200){
            if (self.counter==161) {
                self.currentColor=self.changeColorArr[4];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[4] andColor2:toColor andCurrentColor:self.currentColor];
        }
        else if (self.counter<=240){
            if (self.counter==201) {
                self.currentColor=self.changeColorArr[5];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[5] andColor2:toColor andCurrentColor:self.currentColor];
        }
        else if (self.counter<=280){
            if (self.counter==241) {
                self.currentColor=self.changeColorArr[6];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[6] andColor2:toColor andCurrentColor:self.currentColor];
        }
        else if (self.counter<=320){
            if (self.counter==281) {
                self.currentColor=self.changeColorArr[7];
            }
            newColor=[self getNewColorWithColor1:self.changeColorArr[7] andColor2:toColor andCurrentColor:self.currentColor];
            if (self.counter==320) {
                self.counter=0;
            }
        }
        CBPeripheral *cbp=[timer userInfo];
        NSArray *services=cbp.services;
        CGFloat red,green,blue,alpha;
        [newColor getRed:&red green:&green blue:&blue alpha:&alpha];
        for (CBService *se in services) {
            for (CBCharacteristic *ch in se.characteristics) {
                if ([ch.UUID.UUIDString isEqual:@"FFF1"]) {
                    Byte byte3[] = {22,0,0,0,0};
                    byte3[1]=0.0f;//白色
                    byte3[2]=green*255.0f;//绿色
                    byte3[3]=blue*255.0f;//蓝色
                    byte3[4]=red*255.0f;//红色
                    //传的顺序为 w,g,b,r
                    NSData *data ;
                    data = [NSData dataWithBytes:byte3 length:5];
                    [cbp writeValue:data forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
                }
            }
        }
    }
    else{
        [timer invalidate];
        self.counter=0;
    }
}

//渐变计算颜色
-(UIColor *)getNewColorWithColor1:(UIColor *)color1 andColor2:(UIColor *)color2 andCurrentColor:(UIColor *)curColor{
    if (!self.currentColor) {
        self.currentColor=color1;
    }
    
    CGFloat r,g,b,a;
    CGFloat r1,g1,b1,a1;
    CGFloat r2,g2,b2,a2;
    
    CGFloat subR,subG,subB,subA;
    [color1 getRed:&r green:&g blue:&b alpha:&a];
    [color2 getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    [curColor getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
    
    subR=r1-r;
    subB=b1-b;
    subG=g1-g;
    subA=a1-a;
    
    UIColor *newColor=[UIColor colorWithRed:r2+subR/40.0f green:g2+subG/40.0f blue:b2+subB/40.0f alpha:0];
    self.currentColor=newColor;
    return newColor;
}

@end
